package com.example.pda.common;

import com.example.pda.data.model.GoodItem;
import com.example.pda.data.model.Inventory;

import java.util.List;

import androidx.collection.SparseArrayCompat;

public class CompatUtil {
    private final SparseArrayCompat<GoodItem> mCompat;
    private static CompatUtil util;
    private List<GoodItem> mItemList;
    private boolean isMode23;
    private int mOtherIndex;


    public static CompatUtil getInstance() {
        if (util == null) {
            util = new CompatUtil();
        }
        return util;
    }

    public CompatUtil() {
        mCompat = new SparseArrayCompat<>();
    }

    public void updateLocalData(Inventory inventory) {
        isMode23 = inventory.getBillType().equals("23");
        List<GoodItem> goodItemAll = DaoUtils.getsUtils().getGoodItemAll(inventory.getDjh());
        mCompat.clear();
        if (!Utils.isEmpty(goodItemAll)) {
            for (int i = 0; i < goodItemAll.size(); i++) {
                mCompat.put(i, goodItemAll.get(i));
            }
        }
    }

    public void setItemList(List<GoodItem> mItemList) {
        mOtherIndex = 0;
        this.mItemList = mItemList;
    }

    public List<GoodItem> getItemList() {
        return mItemList;
    }

    public GoodItem getOther(int event) {
        if(isMode23){
            if (event == 1) {
                if (mOtherIndex + 1 >= mItemList.size()) {
                    return null;
                }
                mOtherIndex = mOtherIndex + 1;
            } else if (event == -1) {
                if (mOtherIndex - 1 < 0) {
                    return null;
                }
                mOtherIndex = mOtherIndex - 1;
            }
            GoodItem item = mItemList.get(mOtherIndex);
            for (int i = 0; i < mCompat.size(); i++) {
                GoodItem dbItem = mCompat.get(mCompat.keyAt(i));
                if (dbItem != null && item.getGysname().equals(dbItem.getGysname())
                        && item.getInvcode().equals(dbItem.getInvcode())) {
                    dbItem.setStockList(item.getStockList());
                    dbItem.setHwList(item.getHwList());
                    return dbItem;
                }
            }
            return item;
        } else {
            if (event == 1) {
                if (mOtherIndex + 1 >= mItemList.size()) {
                    return null;
                }
                mOtherIndex = mOtherIndex + 1;
            } else if (event == -1) {
                if (mOtherIndex - 1 < 0) {
                    return null;
                }
                mOtherIndex = mOtherIndex - 1;
            }
            GoodItem item = mItemList.get(mOtherIndex);
            for (int i = 0; i < mCompat.size(); i++) {
                GoodItem dbItem = mCompat.get(mCompat.keyAt(i));
                if (dbItem != null && item.getGysname().equals(dbItem.getGysname())
                        && item.getGyspch().equals(dbItem.getGyspch())
                        && item.getInvcode().equals(dbItem.getInvcode())) {
                    dbItem.setStockList(item.getStockList());
                    dbItem.setHwList(item.getHwList());
                    return dbItem;
                }
            }
            return item;
        }
    }

    public GoodItem getItemBean(int index) {
        if(isMode23){
            /*GoodItem item = mCompat.get(index);
            for (int i = 0; i < mItemList.size(); i++) {
                GoodItem dbItem = mItemList.get(i);
                if (item != null) {
                    if (item.getGysname().equals(dbItem.getGysname())
                            && item.getInvcode().equals(dbItem.getInvcode())) {
                        item.setStockList(dbItem.getStockList());
                        item.setHwList(dbItem.getHwList());
                        return item;
                    }
                }
            }*/
            GoodItem item = mItemList.get(index);
            for (int i = 0; i < mCompat.size(); i++) {
                GoodItem dbItem = mCompat.get(mCompat.keyAt(i));
                if (dbItem != null && item.getGysname().equals(dbItem.getGysname())
                        && item.getInvcode().equals(dbItem.getInvcode())) {
                    dbItem.setStockList(item.getStockList());
                    dbItem.setHwList(item.getHwList());
                    return dbItem;
                }
            }
            return item;
        } else {
            GoodItem item = mItemList.get(index);
            for (int i = 0; i < mCompat.size(); i++) {
                GoodItem dbItem = mCompat.get(mCompat.keyAt(i));
                if (dbItem != null && item.getGysname().equals(dbItem.getGysname())
                        && item.getGyspch().equals(dbItem.getGyspch())
                        && item.getInvcode().equals(dbItem.getInvcode())) {
                    dbItem.setStockList(item.getStockList());
                    dbItem.setHwList(item.getHwList());
                    return dbItem;
                }
            }
            return item;
        }
    }

    public void resetCurIndex(String djh) {
        List<GoodItem> goodItemAll = DaoUtils.getsUtils().getGoodItemAll(djh);
        if (!Utils.isEmpty(goodItemAll)) {
            mCompat.clear();
            for (int i = 0; i < goodItemAll.size(); i++) {
                mCompat.put(i, goodItemAll.get(i));
            }
        }
    }

    public int getOtherIndex() {
        return mOtherIndex;
    }

    public void setOtherIndex(int mOtherIndex) {
        this.mOtherIndex = mOtherIndex;
    }
}
